package my.sorter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sun.codemodel.internal.JAnnotationValue;
import com.sun.tools.javac.util.DiagnosticSource;
import my.sorter.bean.EmergencyCase;
import my.sorter.bean.MyEmail;
import my.sorter.bean.filter.DetailFilter;
import my.sorter.bean.filter.EmergencyCaseFilter;
import my.sorter.dao.baseClient.DetailMapper;
import my.sorter.dao.baseClient.DiagnosesMapper;
import my.sorter.dao.model.Detail;
import my.sorter.dao.model.DetailExample;
import my.sorter.dao.model.Diagnoses;
import my.sorter.dao.model.DiagnosesExample;
import my.sorter.dao.privateClient.PStaffMapper;
import my.sorter.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.lang.model.element.NestingKind;
import java.util.List;
import java.util.UUID;

@Service
public class SortServiceImpl implements SortService {

    @Autowired
    PStaffMapper staffMapper;
    @Autowired
    DetailMapper detailMapper;
    @Autowired
    DiagnosesMapper diagnosesMapper;
    @Autowired
    PStaffMapper pStaffMapper;
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Boolean createDetail(Detail detail) {
        int rows = detailMapper.insert(detail);
        return rows == 1 ? true : false;
    }

    @Override
    public Boolean updateDetail(Detail detail) {
        int rows = detailMapper.updateByPrimaryKeySelective(detail);
        return rows == 1 ? true : false;
    }

    @Override
    public List<Detail> getDetails(DetailFilter filter) {
        DetailExample example = new DetailExample();
        if (!ObjectUtils.isEmpty(filter)) {
            if (!StringUtils.isEmpty(filter.getDetailName())) {
                if (!ObjectUtils.isEmpty(filter.getDetailWeight())) {
                    example
                            .or()
                            .andDDescribeLike("%"+filter.getDetailName()+"%")
                            .andDWeightEqualTo(filter.getDetailWeight());
                    example.or()
                            .andDNameLike("%"+filter.getDetailName()+"%")
                            .andDWeightEqualTo(filter.getDetailWeight());
                } else {
                    example
                            .or()
                            .andDDescribeLike("%"+filter.getDetailName()+"%");
                    example
                            .or()
                            .andDNameLike("%"+filter.getDetailName()+"%");
                }
            }

        }
        List<Detail> details = detailMapper.selectByExample(example);
        return details;
    }

    @Override
    public Boolean deleteDetail(List<String> ids) {
        DetailExample example = new DetailExample();
        example.or().andIdIn(ids);
        int rows = detailMapper.deleteByExample(example);
        return rows > 0 ? true : false;
    }

    @Override
    public Boolean createDiagnoses(Diagnoses diagnoses) {
        //通过外部交互模块获取病人的基本信息

        int rows = diagnosesMapper.insert(diagnoses);
        return rows == 1 ? true : false;
    }

    @Override
    public Boolean diseaseJudge(String diagnosesId, List<JSONObject> caseDetails) {
        int rows=0;
        for (JSONObject cd : caseDetails) {
             rows = pStaffMapper.addDiagnosesDetail(UUID.randomUUID().toString().replace("-",""),diagnosesId, cd.getString("dName"), cd.getString("dDegree"));
        }
        return true;
    }

    @Override
    public Boolean deleteDiseaseJudge(String diagnosesId) {
        int rows = pStaffMapper.deleteDiagnosesDetail(diagnosesId);
        return rows == 1 ? true : false;
    }

    @Override
    public Boolean signJudge(Diagnoses diagnoses) {
        //不更新病例基本信息
        Diagnoses temp = new Diagnoses();
        temp.setId(diagnoses.getId());
        temp.setVitalSigns(diagnoses.getVitalSigns());
        temp.setConsciousness(diagnoses.getConsciousness());
        temp.setPain(diagnoses.getPain());
        temp.setAnaphylaxis(diagnoses.getAnaphylaxis());
        temp.setPressureSore(diagnoses.getPressureSore());
        int rows = diagnosesMapper.updateByPrimaryKeySelective(temp);
        return rows == 1 ? true : false;
    }

    @Override
    public Boolean removeSignJudge(String diagnosesId) {
        Diagnoses diagnoses = new Diagnoses();
        String unregistered = "未登记";
        diagnoses.setId(diagnosesId);
        //不更新病例基本信息
        diagnoses.setpName(null);
        diagnoses.setIdNumber(null);
        diagnoses.setArriveTime(null);
        diagnoses.setArriveChannel(null);
        diagnoses.setIsEmergency(null);
        //病例的护理评估变更为未登记
        diagnoses.setVitalSigns(unregistered);
        diagnoses.setConsciousness(unregistered);
        diagnoses.setPain(unregistered);
        diagnoses.setAnaphylaxis(unregistered);
        diagnoses.setPressureSore(unregistered);
        int rows = diagnosesMapper.updateByPrimaryKeySelective(diagnoses);
        return rows == 1 ? true : false;
    }

    @Override
    public List<EmergencyCase> getDiagnoses(EmergencyCaseFilter emergencyCaseFilter) {
        if (StringUtils.isEmpty(emergencyCaseFilter.getpName())) {
            emergencyCaseFilter.setpName(null);
        }
        if (StringUtils.isEmpty(emergencyCaseFilter.getIdNumber())) {
            emergencyCaseFilter.setIdNumber(null);
        }
        if (StringUtils.isEmpty(emergencyCaseFilter.getStartTime())) {
            emergencyCaseFilter.setStartTime(null);
        }
        if (StringUtils.isEmpty(emergencyCaseFilter.getEndTime())) {
            emergencyCaseFilter.setEndTime(null);
        }
        List<EmergencyCase> emergencyCases = pStaffMapper.getFullDiagnoses(emergencyCaseFilter);
        return emergencyCases;
    }

    @Override
    public Boolean closeDiagnoses(List<String> ids) {
        Diagnoses diagnoses = new Diagnoses();
        diagnoses.setActive(0);
        DiagnosesExample example = new DiagnosesExample();
        example.or().andIdIn(ids);
        int rows = diagnosesMapper.updateByExampleSelective(diagnoses, example);
        return rows > 0 ? true : false;
    }

    @Override
    public Boolean updateDiagnosesBasic(Diagnoses diagnoses) {
        int rows = diagnosesMapper.updateByPrimaryKeySelective(diagnoses);
        return rows == 1 ? true : false;
    }

    @Override
    public Boolean sendEmail(MyEmail email) {
        for (String address:email.getAddress()){
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("SorterTest@163.com");
            message.setTo(address);
            message.setSubject(email.getTitle());
            message.setText(email.getContent());
            javaMailSender.send(message);
        }
        return true;
    }

    @Override
    public Integer calculateOrder(String idNumber) {
        Integer order;
        boolean exists=redisTemplate.hasKey("order");
        if (exists){
            order=1+Integer.parseInt(redisTemplate.opsForValue().get("order").toString());
            redisTemplate.opsForValue().set("order", order);
        }else {
            order=1;
            redisTemplate.opsForValue().set("order", 1);
        }
//        EmergencyCaseFilter filter =new EmergencyCaseFilter();
//        DiagnosesExample example = new DiagnosesExample();
//        example.or().andIdNumberEqualTo(idNumber);
//        List<Diagnoses> ds=diagnosesMapper.selectByExample(example);
//        if (ds.size()>0){
//            Diagnoses d= ds.get(0);
//            filter.setEndTime(d.getArriveTime().getTime());
//        }
//
        return order;
    }
}
